/**
 * 
 */
package com.fox.webapp.web;

import java.util.ArrayList;
import java.util.List;

import org.bson.types.ObjectId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fox.webapp.domain.Broker;
import com.fox.webapp.service.IBrokerService;

/**
 * @author yockii
 *
 */
@Controller
@RequestMapping("broker")
public class BrokerController {

	private static final Logger logger = LoggerFactory.getLogger(BrokerController.class);
	
	@Autowired
	private IBrokerService brokerService;
	
	@RequestMapping(value = {"", "index.html"}, method = RequestMethod.GET)
	public String brokerIndexPage(Model model){
		model.addAttribute("brokerList", brokerService.getAllActivedBroker());
		return "broker/index";
	}
	
	@RequestMapping(value = {"broker/{id}"})
	public String brokerPage(Model model, @PathVariable String id){
		if(ObjectId.isValid(id)){
			Broker b = brokerService.getBroker(id);
			
			model.addAttribute("broker", b);
		}
		return "broker/broker";
	}
	
	@RequestMapping(value = {"allBroker"}, method= {RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public List<Broker> getActiveBrokers(){
		try {
			return brokerService.getAllActivedBroker();
		} catch (Exception e) {
			logger.error("获取所有有效Broker失败");
		}
		return new ArrayList<Broker>();
	}
	
}
